package com.qingcloud.adminbackend.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.ImmutableMap;
import com.qingcloud.adminbackend.entity.config.SysDictionaryConfig;
import com.qingcloud.adminbackend.entity.sensor.CheckSensorDTO;
import com.qingcloud.adminbackend.entity.sensor.SysSensor;
import com.qingcloud.adminbackend.entity.sensor.SysSensorDTO;
import com.qingcloud.adminbackend.mapper.SysDictionaryConfigMapper;
import com.qingcloud.adminbackend.mapper.SysSensorMapper;
import com.qingcloud.adminbackend.service.ISysSensorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备传感器数据表 服务实现类
 * </p>
 *
 * @author luozehua
 * @since 2019-04-16
 */
@Service
public class SysSensorServiceImpl extends ServiceImpl<SysSensorMapper, SysSensor> implements ISysSensorService {

    @Resource
    private SysSensorMapper sysSensorMapper;
    @Resource
    private SysDictionaryConfigMapper sysDictionaryConfigMapper;

    /**
     * 传感器信息
     *
     * @param equipmentId 设备编号
     * @return
     */
    @Override
    public List<SysSensorDTO> sensorDetails(String equipmentId) {
        List<SysSensorDTO> sysSensorDTOS = sysSensorMapper.sensorDetails(equipmentId);
        // 获取字典表
        List<SysDictionaryConfig> sysDictionaryConfigs = sysDictionaryConfigMapper.selectByMap(ImmutableMap.of());
        //设备状态
        final List<SysDictionaryConfig> catagorys = sysDictionaryConfigs.stream().filter(it -> it.getType() == 23).collect(Collectors.toList());
        final List<SysDictionaryConfig> statuss = sysDictionaryConfigs.stream().filter(it -> it.getType() == 15).collect(Collectors.toList());
        sysSensorDTOS.forEach(it -> {
            catagorys.forEach(catagory -> {
                if (catagory.getCode().equals(it.getCatagory())) {
                    it.setCatagoryDetails(catagory.getName());
                }
            });
            statuss.forEach(status -> {
                if (status.getCode().equals(it.getStatus())) {
                    it.setStatusDetails(status.getName());
                }
            });
        });
        return sysSensorDTOS;
    }

    /***
     * 查询传感器ID是否已经存在
     * @param sensorId
     * @return
     */
    @Override
    public Integer existId(String sensorId) {
        QueryWrapper<SysSensor> query = new QueryWrapper<>();
        query.eq("sensor_id", sensorId);
        Long flag = this.baseMapper.selectCount(query);
        if (flag > 0) {
            return 1;
        }
        return 0;
    }

    /**
     * 通过ID查询传感器
     *
     * @param sensorId
     * @return
     */
    @Override
    public SysSensor findById(String sensorId) {
        List<SysSensor> sensors = this.baseMapper.selectByMap(ImmutableMap.of("sensor_id", sensorId));
        if (CollUtil.isNotEmpty(sensors)) {
            return sensors.get(0);
        }
        return null;
    }

    /**
     * 通过传感器ID校验
     *
     * @param sensorId
     */
    @Override
    public CheckSensorDTO checkSensor(String sensorId) {
        CheckSensorDTO result = new CheckSensorDTO();
        //是否
        Integer flag = sysSensorMapper.inUser(sensorId);
        if (flag > 0) {
            //已经在使用
            result.setFlag(-1);
            result.setMsg("传感器已在使用中,请检查输入的编号!");
            return result;
        }
        //闲置传感器
        SysSensor sensor = sysSensorMapper.inIdel(sensorId);
        if (sensor != null) {
            result.setFlag(1);
            result.setSensor(sensor);
            return result;
        }
        result.setFlag(0);
        return result;
    }
}
